<!--
 * @Description: 农事作业分类页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="33"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        :has-cancel="false"
        :has-edit-column="true"
        @showColumn="showColumn"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table @sortChange="changeTableSort" :propList="tableColumn" v-loading="loading" :listData="tableData"
        @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <template v-if="!row.index">
            <el-button
              v-if="judgePermission(33, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(row)"
            ><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button
              v-if="judgePermission(33, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(row)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '农事作业分类信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="60%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item
            label="类型名称:"
            prop="agriculturalWorkTypeName"
            class="w50p"
          >
            <el-input
              v-model="formData.agriculturalWorkTypeName"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="作业方式:"
            prop="method"
            class="w50p"
          >
            <el-select
              v-model="formData.method"
              clearable
            >
              <el-option
                v-for="item in MethodList"
                :key="item.index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="作业效率:"
            prop="efficiency"
            class="w50p"
          >
            <el-input
              v-model="formData.efficiency"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="备注:"
            prop="remark"
            class="w50p"
          >
            <el-input
              v-model="formData.remarks"
              clearable
            />
          </el-form-item>
        </el-form>
        <div
          slot="footer"
          class="dialog-footer"
        >
          <el-button
            class="zh-button-one"
            @click="cancel('formData')"
          >
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        id="tableColumnDialog"
        title="配置列"
        :visible.sync="dialogTableColumnVisible"
      >
        <tableColumn
          v-if="dialogTableColumnVisible"
          ref="tableColumn"
          :tableColumn="allTableColumn"
          @cancelColumn="cancelColumn"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="dialogTableColumnVisible = false">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveColumn"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import Table from '@/components/table/table.vue';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import classificat from "@/api/digital-production/agricultural-work-management/agricultural-work-classification";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
export default {
  name: "AgriculturalWorkClassification",
  components: { SearchForm, Table, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      tableColumn: [
        { prop: 'agriculturalWorkTypeName', label: '作业类型名称' },
        { prop: 'method', label: '作业方式' },
        { prop: 'efficiency', label: '作业效率' },
        { prop: 'operation', label: '操作', slotName: 'operation', width: "120" }
      ],
      formConfig: [
        {
          label: '分类名称',
          prop: 'agriculturalWorkTypeName',
          type: 'input',
        }
      ],
      searchlist: {},
      // 选中的数据项数组 plantingagriculturalWorkTypeId组成 (此数组最多为一个)
      agriculturalWorkTypeIdList: [],
      MethodList: ["手动作业", "机械作业"],
      dialogTitle: null,
      dialogFormVisible: null,
      rules: {
        agriculturalWorkTypeName: [
          { required: true, message: "请输入类型名称", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
    this.getqueryList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        entity: {
          agriculturalWorkTypeName: this.searchlist.agriculturalWorkTypeName
        }
      };
      this.entity = params.entity
      classificat.getWorkTypeList(params).then(res => {
        this.loading = false;
        if (res.code === 1) {
          // this.$message.success('查询成功')
        } else {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        // console.log(this.tableData)
        this.addblankmet();
        this.resetActionBar();
      });
    },
    getqueryList() {
      // classificat.getqueryMethodList().then(res => {
      //   let data = res.data;
      //   this.MethodList = data
      // })
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      if (row.agriculturalWorkTypeId) {
        this.formData = row;
        this.openDialog("edit");
      }
    },
    handleSelectionChange(list) {
      this.agriculturalWorkTypeIdList = [];
      let notvalue = true;
      this.agriculturalWorkTypeIdList = list.map(item => {
        if (item.agriculturalWorkTypeNo) {
          return item.agriculturalWorkTypeNo;
        } else {
          notvalue = false;
          return "";
        }
      });
      console.log(this.agriculturalWorkTypeIdList);
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.agriculturalWorkTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
      // 多个处理为单个数据
      if (this.agriculturalWorkTypeIdList.length > 1) {
        this.$message.warning("员工不可多选删除！");
        this.agriculturalWorkTypeIdList.splice(
          0,
          this.agriculturalWorkTypeIdList.length - 1
        );
        this.delActive = false;
      }
    },
    delmessage() {
      classificat.delmeslist(this.agriculturalWorkTypeIdList).then(res => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.getTableDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(data) {
      // classificat.delmeslist([data.agriculturalWorkTypeNo]).then(
      //   res => {
      //     if (res.code === 1) {
      //       this.$message.success('删除成功')
      //       this.getTableDataList();
      //     } else {
      //       this.$message.error('删除失败')
      //     }
      //   }
      // )
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          classificat.delmeslist([data.agriculturalWorkTypeNo]).then(res => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.getTableDataList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          //
          this.$message("取消删除");
        });
    },
    add() {
      this.formData = {};
      this.openDialog("add");
    },
    save() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            // this.formData.routePointMapperList = this.tagsList;
            classificat.editmeslist(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error("修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            // this.formData.routePointMapperList = this.tagsList;
            // if (this.formData.plantingagriculturalWorkTypeId) {
            //   delete this.formData.plantingagriculturalWorkTypeId
            // }
            classificat.addmeslist(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success("新增成功！");
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error("新增失败！");
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    cancel() {
      this.closeDialog();
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss">
</style>
